<?php
/**
* 班级管理
* @date: 2017年7月28日 下午11:03:08
* @author: hxzlh
*/
namespace app\admin\controller;

use think\Request;
use app\admin\model\Clbum as ClbumMode;
use app\admin\model\StudentClbum;
use app\admin\model\Student;

class Clbum extends Admin
{

    /**
     * XXX
     * 班级管理列表
     */
    public function index()
    {
        $map = [];
        if (Request::instance()->has('name','param')) {
            $name = Request::instance()->param('name');
            if(is_numeric($name)){
                $map['id|name']=   array('like','%'.$name.'%');
            }else{
                $map['name']    =   array('like', '%'.(string)$name.'%');
            }
        }
        if (Request::instance()->has('state','param')) {
            $map['state']    =  Request::instance()->param('state');
        }
        if (Request::instance()->param('day_week')) {
            $map['day_week'] =  Request::instance()->param('day_week');
        }
        $list   = $this->lists('Clbum', $map, 'update_time desc');
        int_to_string($list, ['state'=>['暂停','正常'],'day_week'=>['未选择','周一','周二','周三','周四','周五','周六','周日']]);
        $this->assign('_list', $list);
        $this->assign('meta_title','班级列表');
        return $this->fetch();
    }

    /**
     * XXX
     * 编辑班级
     * @date: 2017年7月27日 下午11:59:13
     * @return: return
     */
    public function edit()
    {
        $model = new ClbumMode();
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'Clbum');
            if(true !== $result){
                return $this->error($result);
            }
            $res = $model->update($data);
            if($res){
                $this->success('编辑成功！', url('index'));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('id');
            empty($id) && $this->error('参数不能为空！');

            $info = $model->find($id);
            $this->assign('_info',       $info);
            $this->assign('meta_title' ,'编辑班级');
            return $this->fetch();
        }
    }
    /**
     * XXX
     * 新增班级
     * @date: 2017年7月27日 下午11:07:29
     * @return mixed|string
     * @return: return
     */
    public function add()
    {
        if(Request::instance()->isPost()){
            //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'Clbum');
            if(true !== $result){
                return $this->error($result);
            }
            $res = ClbumMode::create($data);
            if($data){
                $this->success('新增成功','index');
            } else {
                $this->error($result);
            }
        } else {
            $info = [];
            $this->assign('_info', $info);
            $this->assign('meta_title' ,'新增班级');
            return $this->fetch('edit');
        }
    }
    /**
     * XXX
     * 更改班级状态
     * @date: 2017年7月28日 上午12:25:21
     * @return: return
     */
    public function changeState($method=null)
    {
        $data = Request::instance()->param('id/a');
        $id = array_unique($data);
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbid':
                $this->forbid('clbum', $map );
                break;
            case 'resume':
                $this->resume('clbum', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    /**
     * 班级学员列表
     * @date: 2017年8月2日 上午11:06:49
     * @author: hxz
     * @return mixed|string
     * @return: return
     */
    public function student() 
    {
        $id = Request::instance()->param('id');
        empty($id) && $this->error('参数不能为空！');
        $model = \think\Loader::model('Student','service');
        $name = null;
        if (Request::instance()->has('name','param')) {
            $name = Request::instance()->param('name');
        }
        
        $list = $model->getClbumStudents($id, $name);
        $page = $list->render();
        
        $this->assign('_page', $page);
        $this->assign('_list', $list);
        
        $this->assign('meta_title' ,'班级学员');
        return $this->fetch();
    }
    /**
     * 新增学员
     * @date: 2017年8月4日 下午5:39:35
     * @author: hxz
     * @return void|mixed|string
     */
    public function addStudent() 
    {
        if(Request::instance()->isPost()){ //提交表单
            $data = Request::instance()->post();
            $result = $this->validate($data,'StudentClbum');
            if(true !== $result){
                return $this->error($result);
            }
            $res = StudentClbum::create($data);
            if($res){
                $this->success('新增成功！', url('student',['id'=>$data['clbum_id']]));
            } else {
                $this->error(empty($result) ? '未知错误！' : $result);
            }
        } else {
            $id = Request::instance()->param('clbum_id');
            empty($id) && $this->error('参数不能为空！');
            $where['clbum_id'] = $id;
            $students = StudentClbum::where($where)->column('student_id');
            $map = [];
            if ($students) {
                $map['id'] = ['NOT IN', $students];
            }
            $list = Student::where($map)->column('name', 'id');
            $this->assign('_list', $list);
            $this->assign('meta_title' ,'加入班级');
            return $this->fetch();
        }
    }
    

}
